package week9;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

public class Main4 {
	public static void main(String[] args) 
	{
		Map<String ,List> map = new TreeMap<String ,List>();
		String[] lines = new String[200];
		Scanner in = new Scanner(System.in);
		int a=0;
	    while(in.hasNextLine())
	    {
	    	String line = in.nextLine();
	    	if(line.equals("!!!!!"))
	    		break;
	    	lines[a]=line;
	    	a++;
	    	Scanner scanner = new Scanner(line);
	    	while(scanner.hasNext())
	    	{	
	    		String word = scanner.next();
	    		if(!map.containsKey(word))
	    		{
	    			map.put(word, new ArrayList<Integer>());
	    			map.get(word).add(a);
	    		}
	    		else
	    		{
	    			map.get(word).add(a);
	    		}
	    	}
	    } 
	    for (Map.Entry<String, List> e : map.entrySet()) {  
         	System.out.println(e.getKey()+"="+e.getValue()); 
	    }
	    while(true)
	    {
	    	String[] word=in.nextLine().split(" +");
	    	List<Object> x = new ArrayList<>();
	    	for (int i = 0; i < word.length; i++) 
	    	{
	    		List<Object> y = new ArrayList<>();
	    		y = map.get(word[i]);
	    		if(y==null)
	    			break;
	    		else
	    			x.addAll(y);
			}
	    	Map<Integer, Integer> hs = new HashMap<Integer, Integer>();
	    	for (Object integer : x) {
	            Integer count = 1;
	            if(hs.get(integer) != null) {
	                count = hs.get(integer) + 1;
	            }
	            hs.put((Integer) integer, count);
	        }
	    	List<Integer> g = new ArrayList<Integer>();
	    	for(Map.Entry<Integer,Integer> entry:hs.entrySet())
	    	{
	    		if(entry.getValue()==word.length)
	    		{
	    			Integer h = entry.getKey();
	    			g.add(h);
	    		}
	    	}
	    	Collections.sort(g);
	    	if(!g.isEmpty())
	    	{
	    		System.out.println(g);
	    		for (int i = 0; i < g.size(); i++) {
	    			System.out.println("line "+g.get(i)+":"+lines[(int) g.get(i)-1]);
	    		}
	    	}
	    	else
	    		System.out.println("found 0 results");
	    }
	}	    
}
